package com.athuahua.spzx.controller.cargo;

import cn.hutool.core.date.DateTime;
import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.entity.Brand;
import com.athuahua.spzx.entity.vo.BrandVo;
import com.athuahua.spzx.service.BrandService;
import com.athuahua.spzx.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: huahua
 * @Date: 2024/7/29 0029
 * @Time: 15:02
 * @Description:
 */
@RestController
@RequestMapping("admin/product/brand")
@Tag(name = "品牌管理接口")
@Slf4j
public class BrandController {

    @Resource
    private BrandService brandService;

    //品牌分页查询 admin/product/brand/1/10
    @GetMapping("pageInfo/{page}/{size}")
    @Cacheable(cacheNames = "brandPageInfo", key = "{#p0, #p1}")
    public Result pageInfo(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        log.info("品牌分页查询 page:{}, size:{}", page, size);
        IPage<Brand> list = brandService.getBrandList(page, size);
        BrandVo brandVo = new BrandVo();
        brandVo.setList(list.getRecords());
        brandVo.setTotal((int) list.getTotal());

        return Result.success(brandVo);
    }

    //保存 save
    @PostMapping("save")
    @AutoLog(title = "新增品牌", businessType = 1) //记录日志使用
    @Caching(evict = {
            @CacheEvict(cacheNames = "brandPageInfo",allEntries=true),
            @CacheEvict(cacheNames = "categoryBrandPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "brandFindAll", allEntries = true),
            @CacheEvict(cacheNames = "productPageInfo", allEntries = true)
    })
    public Result saveBrand(@RequestBody Brand brand) {
        log.info("保存品牌:{}", brand);
        brandService.save(brand);
        return Result.success();
    }

    //删除
    @DeleteMapping("delete/{id}")
    @AutoLog(title = "删除品牌", businessType = 2) //记录日志使用
    @Caching(evict = {
            @CacheEvict(cacheNames = "brandPageInfo",allEntries=true),
            @CacheEvict(cacheNames = "categoryBrandPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "brandFindAll", allEntries = true),
            @CacheEvict(cacheNames = "productPageInfo", allEntries = true)})
    public Result deleteBrand(@PathVariable("id") Integer id) {
        log.info("删除品牌id:{}", id);
        brandService.removeById(id);
        return Result.success();
    }

    //修改 update
    @PutMapping("update")
    @AutoLog(title = "修改品牌", businessType = 3) //记录日志使用
    @Caching(evict = {
            @CacheEvict(cacheNames = "brandPageInfo",allEntries=true),
            @CacheEvict(cacheNames = "categoryBrandPageInfo", allEntries = true),
            @CacheEvict(cacheNames = "brandFindAll", allEntries = true),
            @CacheEvict(cacheNames = "productPageInfo", allEntries = true)
    })
    public Result updateBrand(@RequestBody Brand brand) {
        log.info("修改品牌:{}", brand);
        brandService.updateById(brand);
        return Result.success();
    }

    //查询所有品牌
    @GetMapping("findAll")
    @Cacheable(cacheNames = "brandFindAll")
    public Result findAllBrand() {
        log.info("查询所有品牌");
        List<Brand> list = brandService.list(
                new LambdaQueryWrapper<Brand>()
                        .orderByAsc(Brand::getUpdateTime)
        );
        return Result.success(list);
    }
}
